/*
 *  给定一个未排序的整数数组 nums ，找出数字连续的最长序列（不要求序列元素在原数组中连续）的长度。

    请你设计并实现时间复杂度为 O(n) 的算法解决此问题。
 *
 * */


#include <iostream>
#include <vector>
#include <unordered_set>

using namespace std;

class Solution {
public:
    int longestConsecutive(vector<int> &nums) {
        unordered_set<int> m;
        for (const int &num: nums) {
            m.insert(num);
        }

        int res = 0;

        for (const int &num: nums) {
            if (!m.count(num - 1)) {
                int currentNum = num;
                int temp = 1;
                while (m.count(currentNum + 1)) {
                    temp++;
                    currentNum++;
                }

                res = max(res, temp);
            }
        }

        return res;
    }
};

int main() {

    vector<int> nums = {100, 4, 200, 1, 3, 2};
    Solution solution;
    auto res = solution.longestConsecutive(nums);
    cout << res << endl;

    return 0;
}